﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Lenovo.CFI.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
namespace Lenovo.CFI.DAL
{
    /// <summary>
    /// should remove
    /// </summary>
    public class TopicReviewerDa
    {
        public static void InsertTopicReviewer(TopicReviewer topicReviewer)
        {
            InsertTopicReviewer(topicReviewer, DBHelper.GetDatabase(), null);
        }
        public static void InsertTopicReviewer(TopicReviewer topicReviewer, TranscationHelper trans)
        {
            InsertTopicReviewer(topicReviewer, trans.DataBase, trans.Transaction);
        }
        private static void InsertTopicReviewer(TopicReviewer topicReviewer, Database db, DbTransaction trans)
        {
            DbCommand dbCommand = db.GetStoredProcCommand("Usp_InsertTopicReviewer", topicReviewer.TopicID,
                                                          topicReviewer.Reviewer, topicReviewer.Agent);

            try
            {
                if (trans == null)
                    topicReviewer.ID = Convert.ToInt32(db.ExecuteScalar(dbCommand));
                else
                    topicReviewer.ID = Convert.ToInt32(db.ExecuteScalar(dbCommand, trans));
            }
            catch (System.Data.SqlClient.SqlException sex)      // 只捕获SqlException，其余抛出继续传播
            {
                DBHelper.ParseSqlException(sex, false);
            }
        }

        public static void UpdateTopicReviewer(TopicReviewer topicReviewer)
        {
            UpdateTopicReviewer(topicReviewer, DBHelper.GetDatabase(), null);
        }
        public static void UpdateTopicReviewer(TopicReviewer topicReviewer, TranscationHelper trans)
        {
            UpdateTopicReviewer(topicReviewer, trans.DataBase, trans.Transaction);
        }
        private static void UpdateTopicReviewer(TopicReviewer topicReviewer, Database db, DbTransaction trans)
        {
            DbCommand dbCommand = db.GetStoredProcCommand("Usp_UpdateTopicReviewer", topicReviewer.ID, topicReviewer.TopicID,
                                                          topicReviewer.Reviewer, topicReviewer.Agent);

            try
            {
                if (trans == null)
                    db.ExecuteNonQuery(dbCommand);
                else
                    db.ExecuteNonQuery(dbCommand, trans);
            }
            catch (System.Data.SqlClient.SqlException sex)      // 只捕获SqlException，其余抛出继续传播
            {
                DBHelper.ParseSqlException(sex, false);
            }
        }

        public static void DeleteTopicReviewer(int topicID)
        {
            DeleteTopicReviewer(topicID, DBHelper.GetDatabase(), null);
        }
        public static void DeleteTopicReviewer(int topicID, TranscationHelper trans)
        {
            DeleteTopicReviewer(topicID, trans.DataBase, trans.Transaction);
        }
        private static void DeleteTopicReviewer(int topicID, Database db, DbTransaction trans)
        {
            DbCommand dbCommand = db.GetStoredProcCommand("Usp_DeleteTopicReviewer", topicID);

            try
            {
                if (trans == null)
                    db.ExecuteNonQuery(dbCommand);
                else
                    db.ExecuteNonQuery(dbCommand, trans);
            }
            catch (System.Data.SqlClient.SqlException sex)      // 只捕获SqlException，其余抛出继续传播
            {
                DBHelper.ParseSqlException(sex, true);
            }
        }

        public static List<TopicReviewer> GetTopicReviewerList(int topicID)
        {
            Database db = DBHelper.GetDatabase();

            DbCommand dbCommand = db.GetStoredProcCommand("Usp_GetTopicReviewerList", topicID);

            List<TopicReviewer> reviewers = new List<TopicReviewer>();
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                while (dataReader.Read())
                {
                    reviewers.Add(PopulateTopicReviewer(dataReader));
                }
            }

            return reviewers;
        }

        private static TopicReviewer PopulateTopicReviewer(IDataReader dataReader)
        {
            TopicReviewer reviewer = new TopicReviewer(dataReader.GetInt32(0), dataReader.GetInt32(1),
                                                       dataReader.GetString(2), dataReader.GetString(3));

            return reviewer;
        }
    }
}
